<?php

class Cart extends My_Controller
{
	public function __construct()
    {
        parent::__construct();
        $this->load->model('m_item');
    }

    public function add_cart()
    {
        $masp = $this->input->post('productid');
        $row = $this->m_item->get_item_by_id($masp);
        $data['hero'] = $this->m_item->get_all_hero();
        $price1 = $row->PRICEVN;
        if($row->PRICE != '')
        {
            $price1 = $row->PRICE;
        }

        $saleoff = 1;
        if($row->IS_SALE == 0)
        {
            $sample_data = $this->m_item->get_new_saleoff_chung();
            $saleoff = $sample_data[0]->SAMPLE_VALUE;
        }
        else if($row->IS_SALE == 1)
        {
            $saleoff = $this->m_item->get_new_saleoff_by_itemid($masp);
        }


        $data = array(
            'id' => $masp,
            'qty' => 1,
            'price' => ($price1*$saleoff),
            'name' => $row->ITEM_NAME,
            'image' => $row->ITEM_IMAGE       
        );
        $this->cart->insert($data);
        $data['item_quantity'] = $this->cart->total_items();
        $data['total_money'] = $this->cart->total();
        $this->load->model('m_item');
        $data['hero'] = $this->m_item->get_all_hero(); 
       	$this->load->view('Cart/Shop_Cart', $data);
    }

    public function destroy_cart()
    {
        $this->cart->destroy();
    }

    public function load_cart()
    {
        if($this->input->post('capnhat')!= null)
        {
            $i = 1;
            $mang = array();
            
            foreach($this->cart->contents() as $items)
            {
                $rowid = $i . '_rowid';
                $qty = $i . '_qty';
                if($items['rowid']== $this->input->post($rowid) && $items['qty'] != $this->input->post($qty))
                {
                    $data = array('rowid'=>$items['rowid'],'qty'=> $this->input->post($qty));
                    $this->cart->update($data);
                }
                $i++;
            }
        }
        //$this->data['path'] = array('Cart/Shop_Cart');
        $this->load->model('m_item');
        $data['hero'] = $this->m_item->get_all_hero();     
        $this->load->view('Cart/Shop_Cart', $data);
    }

    function test()
    {
        $this->load->view('layout/My_Profile');
    }
}

?>